//
//  UIButton+Extension.m
//  CFFinanceProject
//
//  Created by chunfeng on 2018/4/9.
//  Copyright © 2018年 xiaowen. All rights reserved.
//

#import "UIButton+Extension.h"

#import <objc/runtime.h>
 
@interface UIButton ()

@property(nonatomic, strong)LKLUIButtonClickBlock clickBlock;

@end

@implementation UIButton (LKLExtension)


- (void)lkl_addClickBlock:(LKLUIButtonClickBlock)block{
    self.clickBlock = block;
}

- (void)lkl_UIButtonEvent: (UIButton *)sender{
    if (self.clickBlock) {
        self.clickBlock(sender);
    }
}

#pragma mark Getter/Setter方法
- (void)setLkl_title:(NSString *)lkl_title{
    [self setTitle: lkl_title forState: UIControlStateNormal];
}

-(void)setLkl_select_title:(NSString *)lkl_select_title{
    [self setTitle: lkl_select_title forState: UIControlStateSelected];
    
}

-(void)setLkl_select_titleColor:(UIColor *)lkl_select_titleColor{
    [self setTitleColor: lkl_select_titleColor forState: UIControlStateSelected];
}

-(void)setLkl_image:(UIImage *)lkl_image{
    [self setImage:lkl_image forState:UIControlStateNormal];
    
}

-(NSString *)lkl_select_title{
    return self.lkl_select_title;
}

-(UIColor *)lkl_select_titleColor{
    return self.lkl_select_titleColor;
}

-(UIImage *)lkl_image{
    return self.lkl_image;
}

-(UIImage *)lkl_select_image{
    return self.lkl_select_image;
}

-(void)setLkl_select_image:(UIImage *)lkl_select_image{
    [self setImage:lkl_select_image forState:UIControlStateSelected];
}

- (NSString *)lkl_title{
    return self.currentTitle;
}
- (void)setLkl_titleColor:(UIColor *)lkl_titleColor{
    [self setTitleColor: lkl_titleColor forState: UIControlStateNormal];
}
- (UIColor *)lkl_titleColor{
    return self.titleLabel.textColor;
}
- (void)setLkl_font:(UIFont *)lkl_font{
    self.titleLabel.font = lkl_font;
}
- (UIFont *)lkl_font{
    return self.titleLabel.font;
}

static const char LKLUIButtonBackgroundColorKey;
- (void)setLkl_backgroundColor:(UIColor *)lkl_backgroundColor{
    // 存储新的
    [self willChangeValueForKey:@"lkl_backgroundColor"]; // KVO
    objc_setAssociatedObject(self, &LKLUIButtonBackgroundColorKey,
                             lkl_backgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"lkl_backgroundColor"]; // KVO
    
    [self setBackgroundImage: [UIImage lkl_ImageWithColor: lkl_backgroundColor] forState: UIControlStateNormal];
}

- (UIColor *)lkl_backgroundColor{
    return objc_getAssociatedObject(self, &LKLUIButtonBackgroundColorKey);
}



- (void)setClickBlock:(LKLUIButtonClickBlock)clickBlock{
    if (clickBlock != self.clickBlock) {
        
        /**
         *  两种方式的runtime
         */
        objc_setAssociatedObject(self, @selector(clickBlock), clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
        
        [self addTarget: self action: @selector(lkl_UIButtonEvent:) forControlEvents: UIControlEventTouchUpInside];
        if (!clickBlock) {
            [self removeTarget: self action: @selector(lkl_UIButtonEvent:) forControlEvents: UIControlEventTouchUpInside];
        }
    }
}

- (LKLUIButtonClickBlock)clickBlock{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)lkl_layoutButtonWithEdgeInsetsStyle:(YPButtonEdgeInsetsStyle)style
                            imageTitleSpace:(CGFloat)space{
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.currentImage.size.width * 1.5;
    CGFloat imageHeight = self.currentImage.size.height * 1.5;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case YPButtonEdgeInsetsStyleImageAtTop: {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space, 0);
        }
            break;
        case YPButtonEdgeInsetsStyleImageAtLeft: {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space, 0, space);
            labelEdgeInsets = UIEdgeInsetsMake(0, space, 0, -space);
        }
            break;
        case YPButtonEdgeInsetsStyleImageAtBottom: {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space, -imageWith, 0, 0);
        }
            break;
        case YPButtonEdgeInsetsStyleImageAtRight: {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space, 0, -labelWidth-space);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space, 0, imageWith+space);
        }
            break;
        default:
            break;
    }
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}

@end
